package com.sammy.appmonitor.receivers;

import com.sammy.appmonitor.communication.ParseClient;
import com.sammy.appmonitor.contentprovider.RecordAppHistory;
import com.sammy.appmonitor.contentprovider.TableMessagesOut;
import com.sammy.appmonitor.main.MainActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import com.sammy.utils.MyLog;

public class MonitorApplicationsReceiver extends BroadcastReceiver {
	
	private static final String TAG = "MonitorApplicationsReceiver.";
	
	@Override
	public void onReceive(Context context, Intent intent) {

		Uri uri = intent.getData();
		String pkg = uri != null ? uri.getSchemeSpecificPart() : null;
		if( intent.getAction().equals(android.content.Intent.ACTION_PACKAGE_ADDED)||
			intent.getAction().equals(android.content.Intent.ACTION_PACKAGE_REMOVED)||
			intent.getAction().equals(android.content.Intent.ACTION_PACKAGE_REPLACED) )
		{
			MyLog.d(MainActivity.PROCESS_TAG,TAG+"onReceive: " + pkg);
			
			RecordAppHistory record = new RecordAppHistory();
			record.setAppName(pkg);
			record.setActionType(intent.getAction());
			TableMessagesOut.insertRecord(context, record);
			ParseClient.getInstance().consumeQueue(context);
		}		
	}

}
